package filter;

import javax.servlet.*;
import java.io.IOException;

public class CharacterEncodingFilter implements Filter {
    /**
     * 服务器启动时初始化
     */
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        Filter.super.init(filterConfig);
    }

    /**
     * 过滤器中的代码在过滤特定请求都会执行
     * 必须要让过滤器继续通行
     */
    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=UTF-8");

        System.out.println("Filter执行前");
        chain.doFilter(request, response);
        System.out.println("Filter执行后");

    }

    /**
     * 服务器关闭时会销毁
     */
    @Override
    public void destroy() {
        System.out.println("Filter销毁");
        Filter.super.destroy();
    }
}
